<?php
class GraphController extends Zend_Controller_Action
{

    public function init()
    {
        Zend_Loader::loadClass('My_Graph');
        Zend_Loader::loadClass('My_Link');
        Zend_Loader::loadClass('My_Wertex');
        Zend_Loader::loadClass('My_Object');

        $this->table = new Default_Model_DbTable_Graph();
        $info = $this->table->info();
        $this->primary = $info['primary'][1];
        $this->cols = $info['cols'];

        $this->controller = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();

        $this->urlParams = null;
        if (strlen($this->_getParam('by'))) {
            $this->urlParams .= '/by/'.$this->_getParam('by');
        }
        if (strlen($this->_getParam('sort'))) {
            $this->urlParams .= '/sort/'.$this->_getParam('sort');
        }
        if (strlen($this->_getParam('page'))) {
            $this->urlParams .= '/page/'.$this->_getParam('page');
        }

        $this->form = new Default_Form_Graph();

        Zend_Loader::loadClass('My_Graph');
        $this->graph = new My_Graph();

    }

    public function indexAction()
    {
        $result = $this->graph->getAll();
        if($this->_getParam('graph')){
            $result = $this->table->find($this->_getParam('graph'));
            $result = $result->toArray();
        }

        $paginator = Zend_Paginator::factory($result);
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
        $this->view->paginator = $paginator;


        $this->view->fields = array(
                'id'     => 'ID',
                'name'  => 'Nosaukums',
                'first' => 'Sākuma virsotne',
            );

        $editUrl = $this->view->url(
            array(
                'controller' => $this->controller,
                'action' => 'edit',
                'id' => '{ID}'
            )
        );

        $deleteUrl = $this->view->url(
            array(
                'controller' => $this->controller,
                'action' => 'delete',
                'id' => '{ID}'
            )
        );

        $scenarioUrl = $this->view->url(
            array(
                'controller' => 'scenario',
                'action' => 'index',
                'graph' => '{ID}'
            )
        );

        $this->view->options = array(
            'label' => 'Opcijas',
            'primary' => $this->primary,
            'options' => array(
                'Labot' => $editUrl,
                'Dzēst' => $deleteUrl,
                'Scenārijs' => $scenarioUrl
            )
        );

        //$this->graph->setId(1);
        //echo $this->graph->getFirst();
    }

    public function viewLinksAction() {

        $result = $link->fetchAll(
            $link->select()
                //->where('bug_status = ?', 'NEW')
                ->order($this->_getParam('by').' '.$this->_getParam('sort'))
            );

        $paginator = Zend_Paginator::factory($result->toArray());
        $paginator->setItemCountPerPage(10);
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));

        $this->view->fields = array(
                'id'     => 'ID',
                'start'  => 'Sākuma virsotne',
                'end'    => 'Beigu virsotne',
                'weight' => 'Svars',
            );

        $this->view->paginator = $paginator;
    }

    public function addAction()
    {
        // validate form and save new data
        if ($this->_request->isPost()) {
            $values = $this->_request->getPost();

            // prepare values
            foreach ($this->cols AS $column) {
                $data[$column] = $values[$column];
            }
            

            $wertex = new My_Wertex();
            $parentVertexId = $wertex->insert();
            $childVertexId  = $wertex->insert();

            $link = new My_Link();
            $link->setStart($wertex->insert());
            $link->setEnd($wertex->insert());

            $link->insert();
            $data['first'] = $link->getStart();

            if ($this->form->isValid($values)) {
                $this->table->insert($data);
                $this->_forward('index', null, null, array('message' => 'Veiksmīgi pievienots'));
            } else {
                $this->form->populate($values);
            }
        }

        $this->view->form = $this->form;
    }

    public function editAction()
    {
        $editId = $this->_getParam('id');

        // load row data and save to form
        if (!empty($editId)) {
            $result = $this->table->find($editId);
            $data = $result->toArray();
            $this->form->populate($data[0]);
            unset($data);
        }

        // validate form and save new data
        if ($this->_request->isPost()) {
            $values = $this->_request->getPost();

            // prepare values
            foreach ($this->cols AS $column) {
                $data[$column] = $values[$column];
            }

            if ($this->form->isValid($values) AND !empty($editId)) {
                $where = $this->table->getAdapter()->quoteInto($this->primary.' = ?', $editId);
                $this->table->update($data, $where);

                $this->_forward('index', null, null, array('message' => 'Veiksmīgi saglabāts'));

            } else {
                $this->form->populate($values);
            }
        }
        $this->view->form = $this->form;
    }

    public function deleteAction()
    {
        $editId = $this->_getParam('id');

        $where = $this->table->getAdapter()->quoteInto($this->primary.' = ?', $editId);
        $result = $this->table->delete($where);
        $this->_forward('index', null, null, array('message' => 'Veiksmīgi izdzēsts'));
    }

}


